/**
 * 封装请求库
 * */
// const baseServerUrl = 'http://localhost:3000/mpvueBooks';
const baseServerUrl = 'https://www.chensai.club/mpvueBooks';

function request ({ url, method = 'GET', data = {} }) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${baseServerUrl}${url}`,
      method,
      data,
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
}

// 统一处理返回结果
export default async function (...args) {
  try {
    const res = await request(...args);
    const { data: { code, data: deepData } } = res;
    if (code === 200) return [deepData, null];
    throw deepData; // 跑出异常
  } catch (err) {
    return [null, err];
  }
}